;(function () {
  new Vue({
    el: '#bill-config',
    data: {
      billState: [{name: '开启', code: 'Y'}, {name: '关闭', code: 'N'}],
      billStateActive: 'N',
      selDate: '请选择时间',
      day: null,
      flag: false
    },
    computed: {
      dateDr: function () {
        var result = [];
        for (var i = 0; i < 48; i++) {
          if (i%2) {
            var x = Math.ceil((i-1)/2);
            x = x < 10 ? '0' + x + ':30' : '' + x + ':30';
            result.push(x)
          }
          if (i%2 == 0) {
            var x = i/2;
            x = x < 10 ? '0' + x + ':00' : '' + x + ':00';
            result.push(x)
          }
        }
        return result;
      }
    },
    methods: {
      billStateSelect: function (code) {
        this.billStateActive = code;
      },
      seledDate: function (time) {
        this.selDate = time;
      },
      getTimes: function () {
        var self = this;
        hlhttp.get({
          url: 'bill/submit/config',
          success: function (res) {
            if (!res.time) {
              self.flag = false;
              return;
            };
            if (res.time) {
              self.flag = true;
              self.selDate = res.time;
              self.day = res.day;
              self.billStateActive = res.valid;
            }
          }
        })
      },
      saveSet: function () {
        var self = this;
        if (Number(self.day) < 0) {
          hl.toast('天数不能小于0')
          return
        }
        if (self.selDate == 'Y') {
          if (self.selDate == '请选择时间') {
            hl.toast('请选择时间')
            return
          }
        }
        hlhttp.post({
          url: 'bill/submit/config',
          data: {
            time: self.selDate,
            day: self.day || '0',
            valid: self.billStateActive
          },
          success: function () {
            hl.toast('保存成功')
          }
        })
      },
      editSet: function () {
        var self = this;
        if (Number(self.day) < 0) {
          hl.toast('天数不能小于0')
          return
        }
        if (self.selDate == 'Y') {
          if (self.selDate == '请选择时间') {
            hl.toast('请选择时间')
            return
          }
        }
        hlhttp.put({
          url: 'bill/submit/config',
          data: {
            time: self.selDate,
            day: self.day || '0',
            valid: self.billStateActive
          },
          success: function () {
            hl.toast('编辑成功')
          }
        })
      },
      submitSet: function () {
        if (this.flag) {
          this.editSet()
        }
        if (!this.flag) {
          this.saveSet()
        }
      }
    },
    mounted: function () {
      hlSelectUi(true)
      this.getTimes()
    }
  })
} ())
